package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.User;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Description UserController
 * @Author apeng
 * @Date 2024-10-24
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户管理")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation("用户分页查询")
    @ApiOperationSupport(includeParameters = {"userDto","pageNum","pageSize"})
    public ResponseResult<PageResponse<UserVo>> findUserVoPage(@RequestBody UserDto userDto,
                                                               @PathVariable Integer pageNum,
                                                               @PathVariable Integer pageSize){
        log.info("用户管理分页查询：{}，{}，{}",userDto,pageNum,pageSize);
        return userService.findUserVoPage(userDto,pageNum,pageSize);
    }

    //用户新增
    @PutMapping
    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "userDto",value = "新增用户dto",dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {"realName",
            "roleVoIds","deptNo","postNo","dataState","email","mobile"})
    public ResponseResult insertUser(@RequestBody UserDto userDto){
        return userService.insertUser(userDto);
    }


    //用户修改
    @ApiOperation("修改用户")
    @PatchMapping
    @ApiOperationSupport(includeParameters = {"userDto"})
    @ApiImplicitParam(name = "userDto",value = "修改用户dto",dataType = "UserDto")
    public ResponseResult updateUser(@RequestBody UserDto userDto){
        return userService.updateUser(userDto);
    }

    @ApiOperation("用户列表")
    @ApiOperationSupport(includeParameters = {"userDto.realName"})
    @ApiImplicitParam(name = "userDto",value = "修改用户dto",dataType = "UserDto")
    @PostMapping("/list")
    public ResponseResult<UserVo> findUserVoList(@RequestBody UserDto userDto){
        return userService.findUserVoList(userDto);
    }


    @ApiOperation("启用或禁用用户")
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperationSupport(includeParameters = {"id", "status"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "Long", required = true, paramType = "path"),
            @ApiImplicitParam(name = "status", value = "用户状态", dataType = "String", required = true, paramType = "path")
    })
    public ResponseResult updateUserStatus(@PathVariable Long id, @PathVariable String status) {
        userService.updateUserStatus(id, status);
        return  ResponseResult.success();
    }

    @ApiOperation("删除用户")
    @ApiOperationSupport(includeParameters = {"userId"})
    @ApiImplicitParam(name = "userId",value = "删除用户",dataType = "Long",required = true,paramType = "path")
    @DeleteMapping("remove/{userId}")
    public ResponseResult deleteUser(@PathVariable Long userId){
        return userService.deleteUser(userId);
    }


    //重置密码
    @ApiOperation("重置密码")
    @ApiOperationSupport(includeParameters = {"userId"})
    @ApiImplicitParam(name = "userId",value = "重置密码",dataType = "Long",required = true,paramType = "path")
    @PostMapping("reset-passwords/{userId}")
    public ResponseResult resetPassword(@PathVariable Long userId){
        return userService.resetPassword(userId);
    }
}
